<template>
  <el-dialog
    :title="!dataForm.userId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()">
    <el-form-item label="用户昵称" prop="userNick">
      <el-input v-model="dataForm.userNick" placeholder="用户昵称"></el-input>
    </el-form-item>
    <el-form-item label="用户账号" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="用户账号"></el-input>
    </el-form-item>
    <el-form-item label="用户密码" prop="userPassword">
      <el-input v-model="dataForm.userPassword" placeholder="用户密码"></el-input>
    </el-form-item>
    <el-form-item label="用户电话" prop="userTel">
      <el-input v-model="dataForm.userTel" placeholder="用户电话"></el-input>
    </el-form-item>
    <el-form-item label="用户邮箱" prop="userEmail">
      <el-input v-model="dataForm.userEmail" placeholder="用户邮箱"></el-input>
    </el-form-item>
    <el-form-item label="用户身份证" prop="userIdCard">
      <el-input v-model="dataForm.userIdCard" placeholder="用户身份证"></el-input>
    </el-form-item>
    <el-form-item label="用户年龄" prop="userAge">
      <el-input v-model="dataForm.userAge" placeholder="用户年龄"></el-input>
    </el-form-item>
    <el-form-item label="用户地址" prop="userAddress">
      <el-input v-model="dataForm.userAddress" placeholder="用户地址"></el-input>
    </el-form-item>
    <el-form-item label="用户性别" prop="userSex">
      <el-input v-model="dataForm.userSex" placeholder="用户性别"></el-input>
    </el-form-item>
    <el-form-item label="用户状态(0代表禁用,1正常,2违法被封)" prop="userStatus">
      <el-input v-model="dataForm.userStatus" placeholder="用户状态(0代表禁用,1正常,2违法被封)"></el-input>
    </el-form-item>
    <el-form-item label="用户创建时间" prop="userCreateTime">
      <el-input disabled v-model="dataForm.userCreateTime" placeholder="用户创建时间"></el-input>
    </el-form-item>
    <el-form-item label="用户封禁时间" prop="userWfTime">
      <el-input disabled v-model="dataForm.userWfTime" placeholder="用户封禁时间"></el-input>
    </el-form-item>
    <el-form-item label="用户是否在线(0代表离线,1代表在线)" prop="userOnline">
      <el-input disabled v-model="dataForm.userOnline" placeholder="用户是否在线(0代表离线,1代表在线)"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          userId: 0,
          userNick: '',
          userName: '',
          userPassword: '',
          userTel: '',
          userAva: '',
          userEmail: '',
          userIdCard: '',
          userAge: '',
          userAddress: '',
          userSex: '',
          userStatus: '',
          userCreateTime: '',
          userWfTime: '',
          userOnline: ''
        },
        dataRule: {
          userNick: [
            { required: true, message: '用户昵称不能为空', trigger: 'blur' }
          ],
          userName: [
            { required: true, message: '用户账号不能为空', trigger: 'blur' }
          ],
          userPassword: [
            { required: true, message: '用户密码不能为空', trigger: 'blur' }
          ],
          userTel: [
            { required: true, message: '用户电话不能为空', trigger: 'blur' }
          ],
          userAva: [
            { required: true, message: '用户头像不能为空', trigger: 'blur' }
          ],
          userEmail: [
            { required: true, message: '用户邮箱不能为空', trigger: 'blur' }
          ],
          userIdCard: [
            { required: true, message: '用户身份证不能为空', trigger: 'blur' }
          ],
          userAge: [
            { required: true, message: '用户年龄不能为空', trigger: 'blur' }
          ],
          userAddress: [
            { required: true, message: '用户地址不能为空', trigger: 'blur' }
          ],
          userSex: [
            { required: true, message: '用户性别不能为空', trigger: 'blur' }
          ],
          userStatus: [
            { required: true, message: '用户状态(0代表禁用,1正常,2违法被封)不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.userId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.userId) {
            this.$http({
              url: this.$http.adornUrl(`/xy/user/info/${this.dataForm.userId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userNick = data.xyUser.userNick
                this.dataForm.userName = data.xyUser.userName
                this.dataForm.userPassword = data.xyUser.userPassword
                this.dataForm.userTel = data.xyUser.userTel
                this.dataForm.userAva = data.xyUser.userAva
                this.dataForm.userEmail = data.xyUser.userEmail
                this.dataForm.userIdCard = data.xyUser.userIdCard
                this.dataForm.userAge = data.xyUser.userAge
                this.dataForm.userAddress = data.xyUser.userAddress
                this.dataForm.userSex = data.xyUser.userSex
                this.dataForm.userStatus = data.xyUser.userStatus
                this.dataForm.userCreateTime = data.xyUser.userCreateTime
                this.dataForm.userWfTime = data.xyUser.userWfTime
                this.dataForm.userOnline = data.xyUser.userOnline
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/xy/user/${!this.dataForm.userId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'userId': this.dataForm.userId || undefined,
                'userNick': this.dataForm.userNick,
                'userName': this.dataForm.userName,
                'userPassword': this.dataForm.userPassword,
                'userTel': this.dataForm.userTel,
                'userAva': this.dataForm.userAva,
                'userEmail': this.dataForm.userEmail,
                'userIdCard': this.dataForm.userIdCard,
                'userAge': this.dataForm.userAge,
                'userAddress': this.dataForm.userAddress,
                'userSex': this.dataForm.userSex,
                'userStatus': this.dataForm.userStatus,
                'userCreateTime': this.dataForm.userCreateTime,
                'userWfTime': this.dataForm.userWfTime,
                'userOnline': this.dataForm.userOnline
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
